package com.spm.librarianmanagersystem.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spm.librarianmanagersystem.mapper.BookMapper;
import com.spm.librarianmanagersystem.mapper.BorrowMapper;
import com.spm.librarianmanagersystem.mapper.FineMapper;
import com.spm.librarianmanagersystem.mapper.MemberMapper;
import com.spm.librarianmanagersystem.pojo.Book;
import com.spm.librarianmanagersystem.pojo.Borrow;
import com.spm.librarianmanagersystem.pojo.Fine;
import com.spm.librarianmanagersystem.pojo.Member;
import com.spm.librarianmanagersystem.service.FineService;
import com.spm.librarianmanagersystem.vo.FineAdminDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("FineService")
public class FineServiceImpl implements FineService {
    @Autowired
    FineMapper mapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    BorrowMapper borrowMapper;

    @Override
    public JSONArray getFinesAdminDetail() {
        List<Fine> fines = mapper.getAllFine();
        System.out.println(fines.size());
        List<FineAdminDetail> result = new ArrayList<>();

        for (Fine fine : fines) {
            Borrow borrow = borrowMapper.getBorrowRecordByNumber(fine.getBorrowId());
            Member member = memberMapper.getMemberByUserId(fine.getUserId());
            Book book = bookMapper.getABook(borrow.getISBN());

            FineAdminDetail detail = new FineAdminDetail();
            detail.setFineInfo(fine);
            detail.setBorrowInfo(borrow);
            detail.setId(member.getId());
            detail.setName(member.getName());
            detail.setBookName(book.getBookName());
            result.add(detail);
        }
        return (JSONArray) JSONArray.toJSON(result);
    }

    @Override
    public boolean updateFine0(Integer borrowingId) {
        mapper.updateFine0(borrowingId);
        return true;
    }

    @Override
    public boolean addFine(Fine fine) {
        mapper.addFine(fine);
        return true;
    }

    @Override
    public JSONArray getFineHistory(String currentUserId) {
        List<Fine> fines = mapper.getFineHistory(currentUserId);
        JSONArray result = new JSONArray();

        for (Fine fine : fines) {
            Borrow borrow = borrowMapper.getBorrowRecordByNumber(fine.getBorrowId());
            Member member = memberMapper.getMemberByUserId(fine.getUserId());
            Book book = bookMapper.getABook(borrow.getISBN());

            JSONObject object =new JSONObject();
            object.put("fine_id", fine.getFineId());
            object.put("ISBN", borrow.getISBN());
            object.put("paymentTime", fine.getPaymentTime());
            object.put("status", fine.getStatus());
            object.put("book_name", book.getBookName());
            object.put("total_amount", fine.getAmount());
            result.add(object);
        }
        return (JSONArray) JSONArray.toJSON(result);
    }
}
